/*
* GMouseWheelControl Class v1.0
*  Copyright (c) 2007, Parvez Akkas, parvezakkas@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class adds a new control to Google Map that allows user to use
* mouse scroll wheel to pan the map horizontally or vertically besides
* simply zooming in/out. If enabled, this new control will also allow the
* user to pan the map automatically to a direction where mouse pointer is
* on the map.
*
* For more info, visit: http://www.pmansLab.com/projects/gmwControl/
*
*/

function GMouseWheelControl()
{
}

GMouseWheelControl.prototype = new GControl() ;

GMouseWheelControl.globals =
{
	gmwImageLocation	: null,
	map					: null,
	mapPos				: null,
	swOptionContainer	: null,
	gmwButtonContainer	: null,
	gmwClickPanInitiatedIcon: null,
	gmwButtonTextEnabled: true ,
	gmwButtonIconEnabled: true ,
	clickPanInitiated	: false,
	clickPanFirstMove	: true,
	
	//selected option would one of the following three
	//GMW_PAN_HORIZONTAL
	//GMW_PAN_VERTICAL
	//GMW_ZOOM
	selectedOption		: null,
	selectedOptionIcon	: null,
	
	clickPanDirectionX	: 0,
	clickPanDirectionY	: 0,
	clickPanOriginX		: 0,
	clickPanOriginY		: 0,
	clickPanNewPosX		: 0,
	clickPanNewPosY		: 0
} ;

GMouseWheelControl.prototype.initialize = function(map)
{
	var globals = GMouseWheelControl.globals ;
	var gmwContainer ;
	var gmwScrollWheelOptionContainer ;
	var button ;
	var buttonText ;
	var jsLocation ;
	
	//get the location of this script
	var scripts = document.getElementsByTagName("script") ;
	for(var i = 0 ; i < scripts.length ; i++)
	{
		var scriptSource = scripts[i].src ;
		var temp = scriptSource.indexOf("gmwControl-v1.0.js") ;
		if(temp >= 0)
		{
			jsLocation = scriptSource.slice(0, temp) ;
			globals.gmwImageLocation = jsLocation + "gmwImages-v1.0/" ;
		}
	}
	
	gmwContainer = document.createElement("div") ;
	gmwContainer.id = "gmwContainer" ;
	this.setControlContainerStyle(gmwContainer) ;
	
	gmwScrollWheelOptionContainer = document.createElement("div") ;
	gmwScrollWheelOptionContainer.id = "gmwScrollWheelOptionContainer" ;
	this.setMenuContainerStyle(gmwScrollWheelOptionContainer) ;
	
	var gmwMenu = document.createElement("ul") ;
	gmwMenu.setAttribute("id", "gmwMenu") ;
	this.setMenuStyle(gmwMenu) ;
	
	var menuItem = document.createElement("li") ;
	menuItem.setAttribute("id", "gmwMenuPanVrt") ;
	menuItem.appendChild(document.createTextNode("Pan Vertically")) ;
	menuItem.enableIcon = globals.gmwImageLocation + "gmwPanVrtSelected.gif" ;
	menuItem.disableIcon = globals.gmwImageLocation + "gmwPanVrt.gif" ;
	gmwMenu.appendChild(menuItem) ;
	this.setMenuItemStyle(menuItem) ;
	GEvent.addDomListener(menuItem, "mouseover", menuItemOnMouseOver) ;
	GEvent.addDomListener(menuItem, "mouseout", menuItemOnMouseOut) ;
	GEvent.addDomListener(menuItem, "click", menuItemPanVrtOnClick) ;
	
	menuItem = document.createElement("li") ;
	menuItem.setAttribute("id", "gmwMenuPanHrz") ;
	menuItem.appendChild(document.createTextNode("Pan Horizontally")) ;
	menuItem.enableIcon = globals.gmwImageLocation + "gmwPanHrzSelected.gif" ;
	menuItem.disableIcon = globals.gmwImageLocation + "gmwPanHrz.gif" ;
	gmwMenu.appendChild(menuItem) ;
	this.setMenuItemStyle(menuItem) ;
	GEvent.addDomListener(menuItem, "mouseover", menuItemOnMouseOver) ;
	GEvent.addDomListener(menuItem, "mouseout", menuItemOnMouseOut) ;
	GEvent.addDomListener(menuItem, "click", menuItemPanHrzOnClick) ;
	
	menuItem = document.createElement("li") ;
	menuItem.setAttribute("id", "gmwMenuZoom") ;
	menuItem.appendChild(document.createTextNode("Zoom In/Out")) ;
	menuItem.enableIcon = globals.gmwImageLocation + "gmwZoomSelected.gif" ;
	menuItem.disableIcon = globals.gmwImageLocation + "gmwZoom.gif" ;
	gmwMenu.appendChild(menuItem) ;
	this.setMenuItemStyle(menuItem) ;
	GEvent.addDomListener(menuItem, "mouseover", menuItemOnMouseOver) ;
	GEvent.addDomListener(menuItem, "mouseout", menuItemOnMouseOut) ;
	GEvent.addDomListener(menuItem, "click", menuItemZoomOnClick) ;
	
	var pushPin = document.createElement("img") ;
	pushPin.setAttribute("id", "gmwOptionMenuDisplayState") ;
	pushPin.setAttribute("src", globals.gmwImageLocation + "gmwPushpinUnpinned.gif") ;
	this.setPushpinStyle(pushPin) ;
	GEvent.addDomListener(pushPin, "mouseover", optionMenuPinOnMouseOver) ;
	GEvent.addDomListener(pushPin, "mouseout", optionMenuPinOnMouseOut) ;
	GEvent.addDomListener(pushPin, "click", optionMenuPinOnClick) ;
	
	gmwScrollWheelOptionContainer.appendChild(gmwMenu) ;
	gmwScrollWheelOptionContainer.appendChild(pushPin) ;
	gmwScrollWheelOptionContainer.isVisible = false ;
	gmwScrollWheelOptionContainer.alwaysOnTop = false ;
	
	//create button
	var gmwButtonContainer = document.createElement("div") ;
	gmwButtonContainer.setAttribute("id", "gmwButtonContainer") ;
	this.setButtonContainerStyle(gmwButtonContainer) ;

	button = document.createElement("div") ;
	buttonText = document.createElement("span") ;
	buttonText.innerHTML = "Scroll Wheel" ;
	button.appendChild(buttonText) ;
	button.setAttribute("id", "gmwBtnScrollWheel") ;
	button.setAttribute("title", "Options to use Mouse Wheel") ;
	button.showOptions = true ;	
	button.icon = globals.gmwImageLocation + "gmwBtnScrollWheel.gif" ;
	gmwButtonContainer.appendChild(button) ;
	this.setButtonStyle(button) ;
	GEvent.addDomListener(button, "mouseup", gmwScrollWheelOnClick) ;
	
	button = document.createElement("div") ;
	buttonText = document.createElement("span") ;
	buttonText.innerHTML = "Click &amp; Pan" ;
	button.appendChild(buttonText) ;
	button.setAttribute("id", "gmwBtnClickPan") ;
	button.setAttribute("title", "Panning map by moving mouse pointer") ;
	button.clicPanEnabled = false ;
	button.icon = globals.gmwImageLocation + "gmwBtnClickPan.gif" ;
	gmwButtonContainer.appendChild(button) ;
	this.setButtonStyle(button) ;
	GEvent.addDomListener(button, "click", gmwClickPanOnClick) ;
	
	var gmwClickPanImg = document.createElement("img") ;
	gmwClickPanImg.setAttribute("id", "imgClickPanInitiated") ;
	gmwClickPanImg.setAttribute("src", globals.gmwImageLocation + "clickPanEnabledImage.gif") ;
	gmwClickPanImg.width = 32 ;
	gmwClickPanImg.height = 32 ;
	this.setClickPanInitiatedIconStyle(gmwClickPanImg) ;
	
	gmwContainer.appendChild(gmwButtonContainer) ;
	gmwContainer.appendChild(gmwScrollWheelOptionContainer) ;
	gmwContainer.appendChild(gmwClickPanImg)

	//add click event to the map to check for middle mouse/ wheel button click
	GEvent.addDomListener(map.getContainer(), "mouseup", mapOnMouseUp) ;
	GEvent.addDomListener(map.getContainer(), "mousemove", mapOnMouseMove) ;
	GEvent.addListener(map, "moveend", mapOnMoveEnd) ;
	
	if(document.attachEvent)
		map.getContainer().attachEvent("onmousewheel", mapOnMouseScroll, false) ;
	else
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", mapOnMouseScroll) ;
		
	map.getContainer().appendChild(gmwContainer) ;
	
	//set global variables
	globals.gmwClickPanInitiatedIcon = gmwClickPanImg ;
	globals.swOptionContainer	= gmwScrollWheelOptionContainer ;
	globals.gmwButtonContainer	= gmwButtonContainer ;
	globals.mapPos				= getElementPosition(map.getContainer()) ;
	globals.map					= map ;
	//add resize event to the window so that when the browser is resized,
	//the map's position is calculated. Map's position is critical to show
	//the menu at point where mouse button was clicked.
	GEvent.addDomListener(window, "resize", GMouseWheelControl.prototype.calculateMapPosition) ;
	
	return gmwContainer ;
}

// By default, the control will appear in the top right corner
GMouseWheelControl.prototype.getDefaultPosition = function()
{
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 30));
}

//===================== PUBLIC METHODS ==========================

GMouseWheelControl.prototype.showScrollWheelOption = function(posX, posY)
{
	var global = GMouseWheelControl.globals ;

	//position the menu at the given location
	if(typeof posX == "number")
		global.swOptionContainer.style.left = posX + "px" ;
	if(typeof posY == "number")
		global.swOptionContainer.style.top = posY + "px" ;
	
	//display the menu
	global.swOptionContainer.style.display = "block" ;
	global.swOptionContainer.isVisible = true ;
}

GMouseWheelControl.prototype.hideScrollWheelOption = function()
{
	var global = GMouseWheelControl.globals ;
	if(!global.swOptionContainer.alwaysOnTop)
	{
		global.swOptionContainer.style.display = "none" ;
		global.swOptionContainer.isVisible = false ;
	}
}

GMouseWheelControl.prototype.scrollWheelOptionVisible = function()
{
	return GMouseWheelControl.globals.swOptionContainer.isVisible ;
}

GMouseWheelControl.prototype.showScrollWheelButton = function()
{
	var btnScrollWheel = GMouseWheelControl.globals.gmwButtonContainer ;
	btnScrollWheel.childNodes[0].style.display = "block" ;
}

GMouseWheelControl.prototype.hideScrollWheelButton = function()
{
	var btnScrollWheel = GMouseWheelControl.globals.gmwButtonContainer ;
	btnScrollWheel.childNodes[0].style.display = "none" ;	
}

GMouseWheelControl.prototype.scrollWheelButtonVisible = function()
{
	var btnScrollWheel = GMouseWheelControl.globals.gmwButtonContainer ;
	if(btnScrollWheel.childNodes[0].style.display == "block")
		return true ;
	else
		return false ;
}

GMouseWheelControl.prototype.calculateMapPosition = function()
{
	var globals = GMouseWheelControl.globals ;
	globals.mapPos = getElementPosition(globals.swOptionContainer.parentNode.parentNode) ;
}

GMouseWheelControl.prototype.getMapPosition = function()
{
	return(GMouseWheelControl.globals.mapPos) ;
}

GMouseWheelControl.prototype.showControlButton = function()
{
	GMouseWheelControl.globals.gmwButtonContainer.style.display = "block" ;
}

GMouseWheelControl.prototype.hideControlButton = function()
{
	GMouseWheelControl.globals.gmwButtonContainer.style.display = "none" ;
}

GMouseWheelControl.prototype.controlButtonVisible = function()
{
	if(GMouseWheelControl.globals.gmwButtonContainer.style.display == "none")
		return false ;
	else
		return true ;
}

GMouseWheelControl.prototype.enableButtonText = function()
{
	var buttons = GMouseWheelControl.globals.gmwButtonContainer.childNodes ;
	
	for(var i = 0 ; i < buttons.length ; i++)
	{
		buttons[i].style.width = "74px" ;
		buttons[i].style.backgroundPosition = "left center" ;
		buttons[i].childNodes[0].style.display = "block" ;
	}
	
	GMouseWheelControl.globals.gmwButtonTextEnabled = true ;
}

GMouseWheelControl.prototype.disableButtonText = function()
{
	var buttons = GMouseWheelControl.globals.gmwButtonContainer.childNodes ;
	
	for(var i = 0 ; i < buttons.length ; i++)
	{
		buttons[i].style.width = "18px" ;
		buttons[i].style.backgroundPosition = "center center" ;
		buttons[i].childNodes[0].style.display = "none" ;
	}
	
	GMouseWheelControl.globals.gmwButtonTextEnabled = false ;
}

GMouseWheelControl.prototype.buttonTextEnabled = function()
{
	return 	GMouseWheelControl.globals.gmwButtonTextEnabled ;
}

GMouseWheelControl.prototype.enableButtonIcon = function()
{
	var buttons = GMouseWheelControl.globals.gmwButtonContainer.childNodes ;
	
	for(var i = 0 ; i < buttons.length ; i++)
	{
		buttons[i].style.backgroundImage = "url(" + buttons[i].icon + ")" ;
		buttons[i].style.paddingLeft = "20px" ;
	}
	
	GMouseWheelControl.globals.gmwButtonIconEnabled = true ;
}

GMouseWheelControl.prototype.disableButtonIcon = function()
{
	var buttons = GMouseWheelControl.globals.gmwButtonContainer.childNodes ;
	
	for(var i = 0 ; i < buttons.length ; i++)
	{
		buttons[i].style.backgroundImage = "" ;
		if(!buttons[i].attachEvent) //appling this padding to IE won't render properly
			buttons[i].style.paddingLeft = "3px" ;
	}
	
	GMouseWheelControl.globals.gmwButtonIconEnabled = false ;
}

GMouseWheelControl.prototype.buttonIconEnabled = function()
{
	return GMouseWheelControl.globals.gmwButtonIconEnabled ;
}

GMouseWheelControl.prototype.enableScrollWheelPanVertical = function()
{
	var optionItems = GMouseWheelControl.globals.swOptionContainer.childNodes[0] ;
	
	GMouseWheelControl.prototype.disableScrollWheelZoom() ;
	GMouseWheelControl.globals.selectedOption = "GMW_PAN_VERTICAL" ;
	//change the icon of this option to enabled
	optionItems.childNodes[0].style.background = "url(" + optionItems.childNodes[0].enableIcon + ") no-repeat left center" ;
	//other options may have been enabled before. change those icon
	optionItems.childNodes[1].style.background = "url(" + optionItems.childNodes[1].disableIcon + ") no-repeat left center" ;
	optionItems.childNodes[2].style.background = "url(" + optionItems.childNodes[2].disableIcon + ") no-repeat left center" ;
}

GMouseWheelControl.prototype.disableScrollWheelPanVertical = function()
{
	var optionItem = GMouseWheelControl.globals.swOptionContainer.childNodes[0].childNodes[0] ;
	GMouseWheelControl.globals.selectedOption = null ;
	//change the icon of this option to disabled
	optionItem.style.background = "url(" + optionItem.disableIcon + ") no-repeat left center" ;
}

GMouseWheelControl.prototype.scrollWheelPanVerticalEnabled = function()
{
	if(GMouseWheelControl.globals.selectedOption == "GMW_PAN_VERTICAL")
		return true ;
	else
		return false ;
}

GMouseWheelControl.prototype.enableScrollWheelPanHorizontal = function()
{
	var optionItems = GMouseWheelControl.globals.swOptionContainer.childNodes[0] ;
	GMouseWheelControl.prototype.disableScrollWheelZoom() ;
	GMouseWheelControl.globals.selectedOption = "GMW_PAN_HORIZONTAL" ;
	//change the icon of this option to enabled
	optionItems.childNodes[1].style.background = "url(" + optionItems.childNodes[1].enableIcon + ") no-repeat left center" ;
	//other options may have been enabled before. change those icon
	optionItems.childNodes[0].style.background = "url(" + optionItems.childNodes[0].disableIcon + ") no-repeat left center" ;
	optionItems.childNodes[2].style.background = "url(" + optionItems.childNodes[2].disableIcon + ") no-repeat left center" ;
}

GMouseWheelControl.prototype.disableScrollWheelPanHorizontal = function()
{
	var optionItem = GMouseWheelControl.globals.swOptionContainer.childNodes[0].childNodes[1] ;
	GMouseWheelControl.globals.selectedOption = null ;
	optionItem.style.background = "url(" + optionItem.disableIcon + ") no-repeat left center" ;
}

GMouseWheelControl.prototype.scrollWheelPanHorizontalEnabled = function()
{
	if(GMouseWheelControl.globals.selectedOption == "GMW_PAN_HORIZONTAL")
		return true ;
	else
		return false ;
}

GMouseWheelControl.prototype.enableScrollWheelZoom = function()
{
	var global = GMouseWheelControl.globals ;
	var optionItems = GMouseWheelControl.globals.swOptionContainer.childNodes[0] ;
	global.selectedOption = "GMW_ZOOM" ;
	global.map.enableScrollWheelZoom() ;	
	//change the icon of this option to enabled
	optionItems.childNodes[2].style.background = "url(" + optionItems.childNodes[2].enableIcon + ") no-repeat left center" ;
	//other options may have been enabled before. change those icon
	optionItems.childNodes[0].style.background = "url(" + optionItems.childNodes[0].disableIcon + ") no-repeat left center" ;
	optionItems.childNodes[1].style.background = "url(" + optionItems.childNodes[1].disableIcon + ") no-repeat left center" ;
}

GMouseWheelControl.prototype.disableScrollWheelZoom = function()
{
	var optionItem = GMouseWheelControl.globals.swOptionContainer.childNodes[0].childNodes[2] ;
	var global = GMouseWheelControl.globals ;
	global.selectedOption = null ;
	global.map.disableScrollWheelZoom() ;
	optionItem.style.background = "url(" + optionItem.disableIcon + ") no-repeat left center" ;
}

GMouseWheelControl.prototype.scrollWheelZoomEnabled = function()
{
	if(GMouseWheelControl.globals.selectedOption == "GMW_ZOOM")
		return true ;
	else
		return false ;
}

GMouseWheelControl.prototype.getSelectedOption = function()
{
	return GMouseWheelControl.globals.selectedOption ;
}

GMouseWheelControl.prototype.enableClickPan = function()
{
	var btnClickPan = GMouseWheelControl.globals.gmwButtonContainer.childNodes[1] ;
	
	btnClickPan.clickPanEnabled = true ;
	btnClickPan.style.border	= "1px inset #bbb" ;
	btnClickPan.style.fontWeight= "bold" ;
}

GMouseWheelControl.prototype.disableClickPan = function()
{
	var btnClickPan = GMouseWheelControl.globals.gmwButtonContainer.childNodes[1] ;
	
	GMouseWheelControl.globals.gmwClickPanInitiatedIcon.style.display = "none" ;
	GMouseWheelControl.globals.clickPanInitiated = false ;
	btnClickPan.clickPanEnabled = false ;
	btnClickPan.style.border	= "1px outset #bbb" ;
	btnClickPan.style.fontWeight= "normal" ;
}

GMouseWheelControl.prototype.clickPanEnabled = function()
{
	return GMouseWheelControl.globals.gmwButtonContainer.childNodes[1].clickPanEnabled ;
}

GMouseWheelControl.prototype.showClickPanButton = function()
{
	var btnClickPan = GMouseWheelControl.globals.gmwButtonContainer ;
	btnClickPan.childNodes[1].style.display = "block" ;
}

GMouseWheelControl.prototype.hideClickPanButton = function()
{
	var btnClickPan = GMouseWheelControl.globals.gmwButtonContainer ;
	btnClickPan.childNodes[1].style.display = "none" ;	
}

GMouseWheelControl.prototype.clickPanButtonVisible = function()
{
	var btnClickPan = GMouseWheelControl.globals.gmwButtonContainer ;
	if(btnClickPan.childNodes[1].style.display == "block")
		return true ;
	else
		return false ;
}

//===================== END OF PUBLIC METHODS ==========================

//==================== CSS For GMouseWheelControl ========================

GMouseWheelControl.prototype.setClickPanInitiatedIconStyle = function(icon)
{
	icon.style.border	= "none" ;
	icon.style.width	= icon.width + "px" ;
	icon.style.height	= icon.height + "px" ;
	icon.style.position	= "absolute" ;
	icon.style.top		= 0 ;
	icon.style.left		= 0 ;
	icon.style.display	= "none" ;
}

GMouseWheelControl.prototype.setControlContainerStyle = function(container)
{
	container.style.height	= "19px" ;
	container.style.border	= "1px solid black" ;
}

GMouseWheelControl.prototype.setButtonContainerStyle = function(btnContainer)
{

}

GMouseWheelControl.prototype.setButtonStyle = function(button)
{
	button.style.display	= "block" ;
	button.style.border		= "1px outset #BBB" ;
	button.style.width		= "74px" ;
	button.style.height		= "15px" ;
	button.style.padding	= "1px 3px 1px 20px" ;
	button.style.color		= "#000" ;
	button.style.font		= "9pt Arial" ;
	button.style.textAlign	= "center" ;
	button.style.cursor		= "pointer" ;
	button.style.overflow	= "hidden" ;
	button.style.background = "#FFF url(" + button.icon + ") no-repeat left center" ;
	
	if(button.addEventListener)
		button.style.cssFloat	= "left" ;
	else
		button.style.styleFloat = "left" ;
}

GMouseWheelControl.prototype.setMenuContainerStyle = function(menuContainer)
{
	menuContainer.style.position	= "absolute" ;
	menuContainer.style.display		= "none" ;
	menuContainer.style.border		= "1px outset #000" ;
	menuContainer.style.width		= "167px" ;
}

GMouseWheelControl.prototype.setMenuStyle = function(menu)
{
	menu.style.margin		= "0" ;
	menu.style.padding		= "1px" ;
	menu.style.borderRight	= "1px solid #06C" ;
	menu.style.listStyleType= "none" ;
	menu.style.width		= "135px" ;
	menu.style.background	= "#FFF url(" + GMouseWheelControl.globals.gmwImageLocation + "swMenuBg.gif) repeat-y top left" ;
}

GMouseWheelControl.prototype.setPushpinStyle = function(pushPin)
{
	pushPin.style.position	= "absolute" ;
	pushPin.style.top		= 0 ;
	pushPin.style.right		= 0 ;
	pushPin.style.border	= "none" ;
	pushPin.style.padding	= "2px" ;
}

GMouseWheelControl.prototype.setMenuItemStyle = function(menuItem)
{
	menuItem.style.padding	= "4px 3px 4px 33px" ;
	menuItem.style.fontFamily = "Vardana, Arial, Helvetica, sans-serif" ;
	menuItem.style.fontSize	= "10pt" ;
	menuItem.style.color	= "#333" ;
	menuItem.style.cursor	= "default" ;
	menuItem.style.background = "url(" + menuItem.disableIcon + ") no-repeat left center" ;
}

//==================== END OF CSS For GMouseWheelControl ========================

function gmwScrollWheelOnClick(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	if((this.showOptions || !GMouseWheelControl.globals.swOptionContainer.isVisible) &&
	   !GMouseWheelControl.globals.swOptionContainer.alwaysOnTop)
	{
		//get the position of this button
		var posX = this.offsetLeft ;
		var posY = this.offsetTop ;
		//show menu at the above location
		GMouseWheelControl.prototype.showScrollWheelOption(posX, posY + this.offsetHeight) ;
		this.showOptions = false ;
	}
	else
	{
		GMouseWheelControl.prototype.hideScrollWheelOption() ;
		this.showOptions = true ;
	}
}

function gmwClickPanOnClick(e)
{
	if(this.clickPanEnabled)
		GMouseWheelControl.prototype.disableClickPan() ;
	else
		GMouseWheelControl.prototype.enableClickPan() ;
}

function gmwPanMap()
{
	var global = GMouseWheelControl.globals ;
	
	if(global.clickPanInitiated)
	{
		global.map.panBy(new GSize(global.clickPanDirectionX , global.clickPanDirectionY )) ;
	}
}

function mapOnMoveEnd(e)
{
	gmwPanMap() ;
}

function mapOnMouseMove(e)
{
	var global = GMouseWheelControl.globals ;
	
	if(global.clickPanInitiated)
	{
		var mousePosX ;
		var mousePosY ;
		var getPos ;
		
		getPos = getMousePosition(e) ;
	
		mousePosX = getPos.posX - global.mapPos.posX ;
		mousePosY = getPos.posY - global.mapPos.posY ;
		global.clickPanNewPosX	= mousePosX ;
		global.clickPanNewPosY	= mousePosY ;
			
		global.clickPanDirectionX = (global.clickPanOriginX - global.clickPanNewPosX)/4 ;
		global.clickPanDirectionY = (global.clickPanOriginY - global.clickPanNewPosY)/4 ;
		
		if(global.clickPanFirstMove)
		{
			global.clickPanFirstMove = false ;
			gmwPanMap() ;
		}
	}
}

function mapOnMouseUp(e)
{
	var global = GMouseWheelControl.globals ;
	var mousePosX ;
	var mousePosY ;
	var getPos ;
	//find out if middle mouse button was clicked or if clicked with a special key
	if(middleMouseButtonClicked(e) || specialKeyPressed(e))
	{
		getPos = getMousePosition(e) ;
		mousePosX = getPos.posX - global.mapPos.posX - global.swOptionContainer.parentNode.offsetLeft ;
		mousePosY = getPos.posY - global.mapPos.posY - global.swOptionContainer.parentNode.offsetTop ;
		
		if(!GMouseWheelControl.prototype.clickPanEnabled())
		{
			//show menu at the above location
			if(!global.swOptionContainer.alwaysOnTop)
			{
				GMouseWheelControl.prototype.showScrollWheelOption(mousePosX, mousePosY) ;
				global.gmwButtonContainer.childNodes[0].showOptions = true ;
			}
		}
		else
		{
			var panInitIcon = global.gmwClickPanInitiatedIcon ;
			//enable clickPanInitate
			if(global.clickPanInitiated)
			{
				global.clickPanInitiated = false ;
				panInitIcon.style.display = "none" ;
				global.clickPanDirectionX = 0 ;
				global.clickPanDirectionY = 0 ;
			}
			else
			{
				global.clickPanInitiated = true ;
				global.clickPanFirstMove = true ;
				panInitIcon.style.left = mousePosX - panInitIcon.width + "px" ;
				panInitIcon.style.top = mousePosY - panInitIcon.height/2 + "px" ;
				panInitIcon.style.display = "block" ;
				global.clickPanNewPosX = getPos.posX - global.mapPos.posX  ;
				global.clickPanNewPosY = getPos.posY - global.mapPos.posY  ;
				global.clickPanOriginX = global.clickPanNewPosX ;
				global.clickPanOriginY = global.clickPanNewPosY ;
			}
		}
	}
	else
	{
		if(!global.swOptionContainer.alwaysOnTop)
			GMouseWheelControl.prototype.hideScrollWheelOption() ;
			
		global.clickPanInitiated = false ;
		global.gmwClickPanInitiatedIcon.style.display = "none" ;
	}
}

function mapOnMouseScroll(e)
{
	var global = GMouseWheelControl.globals ;
	
	if(global.selectedOption != null || global.clickPanInitiated)
	{
		if(e.preventDefault)
			e.preventDefault() ;
		else
			e.returnValue = false ;
	}
	
	if(global.selectedOption == "GMW_PAN_HORIZONTAL" || global.selectedOption == "GMW_PAN_VERTICAL")
	{
	//http://adomas.org/javascript-mouse-wheel/
		var delta = 0;
		
		if(!e) /* For IE. */
			e = window.event;
		if(e.wheelDelta)
		{
			/* IE/Opera. */
			delta = e.wheelDelta/120;
			/** In Opera 9, delta differs in sign as compared to IE.
			 */
			if (window.opera)
				delta = -delta;
		}
		else if(e.detail)
		{
			/** Mozilla case. */
			/** In Mozilla, sign of delta is different than in IE.
			 * Also, delta is multiple of 3.
			 */
			delta = -e.detail/3;
		}
		/** If delta is nonzero, handle it.
		* Basically, delta is now positive if wheel was scrolled up,
		* and negative, if wheel was scrolled down.
		*/
		if(delta)
			panByScrollWheel(delta);
	}
}

function panByScrollWheel(delta)
{
	var global = GMouseWheelControl.globals ;
	
	if(delta > 0)
	{
		//scroll up
		if(global.selectedOption == "GMW_PAN_HORIZONTAL")
		{
			//pan the map horizontally to right
			global.map.panBy(new GSize(50,0)) ;
		}
		else
		{
			//pan the map vertically to downwards
			global.map.panBy(new GSize(0,50)) ;
		}
	}
	else
	{
		//scroll down
		if(global.selectedOption == "GMW_PAN_HORIZONTAL")
		{
			//pan the map horizontally to left
			global.map.panBy(new GSize(-50,0)) ;
		}
		else
		{
			//pan the map vertically to upwards
			global.map.panBy(new GSize(0,-50)) ;
		}
	}
}

function middleMouseButtonClicked(event)
{
	var button ;
	
	if (event.which == null)
	{	//IE case
		if(event.button < 2)
			button = "LEFT" ;
		else if(event.button == 4)
			button = "MIDDLE" ;
		else
			button = "RIGHT" ;
	}
	else
	{
		if(event.which < 2)
			button = "LEFT" ;
		else if(event.which == 2)
			button = "MIDDLE" ;
		else
			button = "RIGHT" ;
	}

	if(button == "MIDDLE")
		return true ;
	else
		return false ;
}

function specialKeyPressed(e)
{
	if(e.shiftKey)
		return true ;
	else
		return false ;
}

function menuItemOnMouseOver()
{
	this.style.padding	= "3px 2px 3px 32px" ;
	this.style.color	= "#000" ;
	this.style.border	= "1px solid #06C" ;
	this.style.backgroundColor = "#fec" ;
}

function menuItemOnMouseOut()
{
	this.style.padding	= "4px 3px 4px 33px" ;
	this.style.color	= "#333" ;
	this.style.border 	= "none" ;
	this.style.backgroundColor = "" ;
}

function menuItemZoomOnClick()
{
	var global = GMouseWheelControl.globals ;
	
	if(global.selectedOption != "GMW_ZOOM")
		GMouseWheelControl.prototype.enableScrollWheelZoom() ;
	else
		GMouseWheelControl.prototype.disableScrollWheelZoom() ;	
}

function menuItemPanHrzOnClick()
{
	var global = GMouseWheelControl.globals ;

	if(global.selectedOption != "GMW_PAN_HORIZONTAL")
		GMouseWheelControl.prototype.enableScrollWheelPanHorizontal() ;
	else
		GMouseWheelControl.prototype.disableScrollWheelPanHorizontal() ;
}

function menuItemPanVrtOnClick()
{
	var global = GMouseWheelControl.globals ;

	if(global.selectedOption != "GMW_PAN_VERTICAL")
		GMouseWheelControl.prototype.enableScrollWheelPanVertical() ;
	else
		GMouseWheelControl.prototype.disableScrollWheelPanVertical() ;	
}

function optionMenuPinOnMouseOver()
{
	this.style.padding = "1px" ;
	this.style.border = "1px solid orange" ;
}

function optionMenuPinOnMouseOut()
{
	this.style.padding = "2px" ;
	this.style.border = "none" ;
}

function optionMenuPinOnClick(e)
{
	var global = GMouseWheelControl.globals ;
	
	if(global.swOptionContainer.alwaysOnTop)
	{
		this.src = global.gmwImageLocation + "gmwPushpinUnpinned.gif" ;
		global.swOptionContainer.alwaysOnTop = false ;
	}
	else
	{
		this.src = global.gmwImageLocation + "gmwPushpinPinned.gif" ;
		global.swOptionContainer.alwaysOnTop = true ;
		GMouseWheelControl.prototype.showScrollWheelOption() ;
	}	
}

function getMousePosition(e)
{
	var mousePosX ;
	var mousePosY ;
	if (!e) var e = window.event;
	if(e.pageX || e.pageY)
	{
		mousePosX = e.pageX ;
		mousePosY = e.pageY ;
	}
	else if(e.clientX || e.clientY)
	{
		mousePosX = e.clientX + (document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft) ;
		mousePosY = e.clientY + (document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop) ;
	}
	
	return {posX: mousePosX, posY: mousePosY} ;
}

function getElementPosition(element)
{
	var elementPosX			= element.offsetLeft ; 
	var elementPosY			= element.offsetTop ; 
	var parentElement		= element.offsetParent ; 
	
	while (parentElement != null )
	{
		elementPosX		+= parentElement.offsetLeft ; // appending left offset of each parent
		elementPosY		+= parentElement.offsetTop ;  
		parentElement = parentElement.offsetParent ;  // until no more offset parents exist
	}
	return {posX:elementPosX, posY:elementPosY} ;
}
